package com.woniu.view;

import com.woniu.controller.*;
import com.woniu.entity.*;
import com.woniu.mapper.OrderItemsMapper;

import java.text.SimpleDateFormat;
import java.util.*;

public class ToMerchantPage {
    public static void merchantPage(Users users) {
        boolean flag = true;
        while (flag){
            System.out.println("_________________________");
            System.out.println("|------欢迎进入商家系统-----|");
            System.out.println("|--------1.商品管理--------|");
            System.out.println("|--------2.订单管理------|");
            System.out.println("|--------3.数据统计-----|");
            System.out.println("|--------4.个人信息-----|");
            System.out.println("|--------5.退出-----------|");
            System.out.println("--------------------------");
            Scanner sc = new Scanner(System.in);
            System.out.println("请选择你的操作：");
            int choice=sc.nextInt();
            switch (choice){
                case 1:
                    System.out.println("商品管理");
                    manageProduct(users);
                    break;
                case 2:
                    System.out.println("订单管理");
                    manageOrder(users);
                    break;
                case 3:
                    System.out.println("数据统计");
                    dataStatistics(users);
                    break;
                case 4:
                    System.out.println("个人信息");
                    viewprofile(users);
                    break;
                case 5:
                    flag = false;//结束循环(退出系统)
                    break;
                default:
                    System.out.println("你输入有误！请重选选择");
                    break;
            }
        }
    }
    /**
     * 个人信息
     * */
    public static void viewprofile(Users users) {
        Power power = PowerController.selectPowerById(users.getPower_id());
        System.out.println("id\t姓名\t密码\t电话\t余额\t身份");
        System.out.println(users.getUser_id()+"\t"+users.getUsername()+"\t"+users.getPwd()+"\t"+users.getTelphone()+"\t"+users.getMoney()+"\t"+power.getPname());
        System.out.println("是否更改个人信息");
        System.out.println("输入y更改");
        System.out.println("输入n不更改");
        Scanner sc = new Scanner(System.in);
        String ch=sc.nextLine();
        if(ch.equals("y")){
            System.out.println("选择要修改的信息");
            System.out.println("1.修改用户名");
            System.out.println("2.修改密码");
            System.out.println("3.修改手机号");
            int choice=sc.nextInt();
            switch (choice){
                case 1:
                    System.out.println("输入修改后的名字");
                    String name = sc.next();
                    UserController.updateUserName(name, users.getUser_id());
                    System.out.println("修改成功");
                    break;
                case 2:
                    System.out.println("输入要修改的密码");
                    String password = sc.next();
                    UserController.updateUserPwd(password,users.getUser_id());
                    System.out.println("修改成功");
                    break;
                case 3:
                    System.out.println("修改要修改的手机号");
                    String phone = sc.next();
                    UserController.updateUserTelphone(phone,users.getUser_id());
                    System.out.println("修改成功");
                    break;
                default:
                    System.out.println("无此操作");
                    break;
            }
        }else if(ch.equals("n")){
            System.out.println("退出");
        }
    }
    /**
     * 数据统计
     * */
    public static void dataStatistics(Users users) {
        List<OrderItems> orderItemsByStatus = OrderItemsCotroller.getOrderItemsByStatus(users.getUser_id(), 5);
        HashSet<Integer> objects = new HashSet<>();
        for (OrderItems orderItems : orderItemsByStatus) {
            objects.add(orderItems.getProduct_id());
        }
        for (Integer object : objects) {
            double totalPrice = 0;
            Integer sum = 0;
            List<OrderItems> orderItemsByPid = OrderItemsCotroller.getOrderItemsByPid(users.getUser_id(), object);
            for (OrderItems orderItems : orderItemsByPid) {
                totalPrice+=orderItems.getPrice();
                sum+=orderItems.getQuantity();
            }
            Product productById = ProductController.getProductById(object);
            System.out.println("商品"+productById.getPname()+"的交易量为:"+sum);
            System.out.println("商品"+productById.getPname()+"交易额为:"+totalPrice);
        }
    }
    /**
     * 商品管理
     * */
    public static void manageProduct(Users users) {
        List<Product> productByUid = ProductController.getProductByUid(users.getUser_id());
        for (Product product : productByUid) {
            System.out.println(product);
        }
        System.out.println("1.上架商品");
        System.out.println("2.编辑商品");
        System.out.println("3.下架商品");
        System.out.println("选择操作");
        Scanner sc = new Scanner(System.in);
        int choice = sc.nextInt();
        switch (choice){
            case 1:
                addProduct(users);
                break;
            case 2:
                updateProduct(users);
                break;
            case 3:
                deleteProduct(users);
                break;
            default:
                System.out.println("无此操作");
                break;
        }
    }
    /**
     * 发布商品
     * */
    public static void addProduct(Users users) {
        Scanner sc = new Scanner(System.in);
        System.out.println("商品名称:");
        String productName = sc.next();
        System.out.println("商品单价:");
        Double productPrice = sc.nextDouble();
        System.out.println("商品数量:");
        int productQuantity = sc.nextInt();
        System.out.println("商品详情");
        String productDesc = sc.next();
        boolean flag = true;
        for (char a : productName.toCharArray()) {
            if(a=='黄' || a=='赌' || a=='毒'){
                flag = false;
                break;
            }
        }
        if(flag){
            WaitPutaway waitPutaway = new WaitPutaway(null,productName,productPrice,productQuantity,productDesc,new Date(),users.getUser_id());
            WaitPutawayController.insertWaitPutaway(waitPutaway);
            System.out.println("以添加至待审核，等待管理员审核");
        }else {
            System.out.println("商品不合格，无法上架");
        }
    }
    /**
     * 编辑商品
     * */
    public static void updateProduct(Users users) {
        System.out.println("请选择你要修改的商品id");
        Scanner sc = new Scanner(System.in);
        int pid = sc.nextInt();
        System.out.println(ProductController.getProductById(pid));
        System.out.println("你要修改该商品的");
        System.out.println("1.单价");
        System.out.println("2.数量");
        int choice = sc.nextInt();
        switch (choice){
            case 1:
                System.out.println("请输入修改后的单价");
                double productPrice = sc.nextDouble();
                ProductController.updateProductPrice(pid,productPrice);
                System.out.println("修改成功");
                break;
            case 2:
                System.out.println("请输入商品要添加的数量");
                int stock = sc.nextInt();
                ProductController.updateProductStock(pid,-stock);
                System.out.println("添加成功");
                break;
            default:
                System.out.println("无此操作");
                break;
        }
    }
    /**
     * 下架商品
     * */
    public static void deleteProduct(Users users) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要下架商品的id");
        int pid = sc.nextInt();
        ProductController.deleteProduct(pid);
        System.out.println("下架成功");
    }

    /**
     * 订单管理
     * */
    public static void manageOrder(Users users) {
        List<OrderItems> orderItemsByMid = OrderItemsCotroller.getOrderItemsByMid(users.getUser_id());
        for (OrderItems orderItems : orderItemsByMid) {
            System.out.println(orderItems);
        }
        Scanner sc = new Scanner(System.in);
        System.out.println("1.查看订单");
        System.out.println("2.发货");
        System.out.println("3.处里退款");
        int choice = sc.nextInt();
        switch (choice){
            case 1:
                checkOrders(users);
                break;
            case 2:
                shipments(users);
                System.out.println("发货完成");
                break;
            case 3:
                salesReturn(users);
                break;
            default:
                System.out.println("无此操作");
                break;
        }
    }
    /**
     * 退款
     * */
    public static void salesReturn(Users users) {
        List<OrderItems> orderItemsByStatus = OrderItemsCotroller.getOrderItemsByStatus(users.getUser_id(), 4);
        for (OrderItems orderItems : orderItemsByStatus) {
            System.out.println(orderItems);
        }
        Scanner sc = new Scanner(System.in);
        System.out.println("是否退款退货");
        System.out.println("输入y退款");
        System.out.println("输入n拒绝退款");
        String ch = sc.next();
        if (ch.equals("y")) {
            Set<Integer> set = new HashSet<>();
            for (OrderItems orderItems : orderItemsByStatus) {
                set.add(orderItems.getOrder_id());
                Orders orders = OrdersController.selectOrdersByOrderID(orderItems.getOrder_id());
                Integer userId = orders.getUser_id();
                ProductController.updateProductStock(orderItems.getProduct_id(),-orderItems.getQuantity());
                UserController.updateUserMoney(userId,-orderItems.getPrice());
                UserController.updateUserMoney(users.getUser_id(),orderItems.getPrice());
                OrdersController.updateOrderstotal(orderItems.getOrder_id(),orderItems.getPrice());
            }
            OrderItemsCotroller.deleteByMid(users.getUser_id(),4);
            for (Integer orderId : set) {
                List<OrderItems> orderItems = OrderItemsCotroller.getOrderItems(orderId);
                if (orderItems.size() == 0) {
                    OrdersController.deleteOrdersByorderId(orderId);
                }
            }
            System.out.println("退款完成");
        }else if (ch.equals("n")) {
            for (OrderItems orderItems : orderItemsByStatus) {
                OrderItemsCotroller.updateOrderItemsByOrderId(orderItems.getOrder_id(), 6);
            }
            List<OrderItems> orderItemsByStatus1 = OrderItemsCotroller.getOrderItemsByStatus(users.getUser_id(), 6);
            Set<Integer> set = new HashSet<>();
            for (OrderItems orderItems : orderItemsByStatus1) {
                set.add(orderItems.getOrder_id());
            }
            for (Integer orderId : set) {
                boolean flag = true;
               List<OrderItems> orderItems = OrderItemsCotroller.getOrderItems(orderId);
               for (OrderItems orderItems1 :orderItems){
                   if(orderItems1.getStatus()!=6){
                       flag = false;
                       break;
                   }
                   if (flag){
                       OrdersController.updateOrdersStatus(orderId,6);
                   }
               }
            }
        }else {
            System.out.println("退出");
        }
    }
    /**
     * 查看订单
     * */
    public static void checkOrders(Users users) {
        Scanner sc = new Scanner(System.in);
        System.out.println("1.查看所有订单");
        System.out.println("2.通过商品名查看订单");
        int choice = sc.nextInt();
        if (choice == 1) {
            List<OrderItems> orderItemsByMid = OrderItemsCotroller.getOrderItemsByMid(users.getUser_id());
            for (OrderItems orderItems : orderItemsByMid) {
                Product productById = ProductController.getProductById(orderItems.getProduct_id());
                Integer status = orderItems.getStatus();
                State state = StateController.selectStateById(status);
                Date createdAt = orderItems.getCreated_at();
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String format = formatter.format(createdAt);
                System.out.println("订单编号\t商品\t数量\t价格\t状态\t下单时间");
                System.out.println(orderItems.getOrder_item_id()+"\t"+productById.getPname()+"\t"+orderItems.getQuantity()+"\t"+orderItems.getPrice()+"\t"+state.getStatus()+"\t"+format);
            }
        }else if (choice == 2) {
            System.out.println("请输入商品名");
            String pname = sc.next();
            Product product = ProductController.selectProductMidName(users.getUser_id(), pname);
             List<OrderItems> orderItemsByPid = OrderItemsCotroller.getOrderItemsByPid(users.getUser_id(), product.getPid());
             for (OrderItems orderItems : orderItemsByPid) {
                 Product productById = ProductController.getProductById(orderItems.getProduct_id());
                 Integer status = orderItems.getStatus();
                 State state = StateController.selectStateById(status);
                 Date createdAt = orderItems.getCreated_at();
                 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                 String format = formatter.format(createdAt);
                 System.out.println("订单编号\t商品\t数量\t价格\t状态\t下单时间");
                 System.out.println(orderItems.getOrder_item_id()+"\t"+productById.getPname()+"\t"+orderItems.getQuantity()+"\t"+orderItems.getPrice()+"\t"+state.getStatus()+"\t"+format);
             }
        }
    }
    /**
     * 发货
     * */
    public static void shipments(Users users) {
        System.out.println("已支付的订单:");
        List<OrderItems> orderItemsByStatus = OrderItemsCotroller.getOrderItemsByStatus(users.getUser_id(), 1);
        for (OrderItems orderItems : orderItemsByStatus) {
            System.out.println(orderItems);
        }
        while (true){
            System.out.println("选择要发货单订单id");
            Scanner sc = new Scanner(System.in);
            int choice = sc.nextInt();
            OrderItemsCotroller.updateOrderItemsByOrderItemId(choice,3);
            List<Orders> allOrders = OrdersController.getAllOrders();
            boolean flag = true;
            for (Orders orders : allOrders) {
                Integer orderId = orders.getOrder_id();
                List<OrderItems> orderItems = OrderItemsCotroller.getOrderItems(orderId);
                //如果订单所以的订单详情都是发货状态，订单也是发货状态
                for (OrderItems orderItem : orderItems) {
                    Integer status = orderItem.getStatus();
                    if(status == 1 || status == 2 || status == 4 || status == 5 || status ==6) {
                        flag = false;
                        break;
                    }
                }
                if(flag){
                    OrdersController.updateOrdersStatus(orders.getOrder_id(),3);
                }
            }
            System.out.println("发货完成,是否继续");
            System.out.println("1.继续");
            System.out.println("2.退出");
            int choice1 = sc.nextInt();
            if (choice1 == 2) {
                break;
            }
        }
    }

    }

